package com.example.administrator.exkotlin.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View

import android.view.View.OnClickListener
import android.widget.Toast
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_test__1.*

class Test_Activity_1 : BaseActivity() {

    val TAG = "Test_Activity_1"
    private val clickListner = MyOnclickListner()
    var context: Context? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test__1)
        context = this.applicationContext
        init()
    }

    private fun init() {
        btn_1.setOnClickListener(clickListner)
        btn_2.setOnClickListener(clickListner)
        btn_3.setOnClickListener(clickListner)
        btn_4.setOnClickListener(clickListner)
        btn_5.setOnClickListener(clickListner)

    }

    private inner class MyOnclickListner : OnClickListener {
        override fun onClick(v: View?) {
            when (v?.id) {
                //Toast测试触发
                R.id.btn_1 -> {
                    Log.i(TAG, "you click btn_1")
                    Toast.makeText(context, "you click btn_1", Toast.LENGTH_SHORT).show()
                }
                R.id.btn_2 -> {
                    Log.i(TAG, "you click btn_2")
                    Toast.makeText(context, "显示跳转到页面2", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(context, Test_Activity_2::class.java))
                }
                R.id.btn_3 -> {
                    Log.i(TAG, "you click btn_3")
                    Toast.makeText(context, "隐式跳转到页面2", Toast.LENGTH_SHORT).show()
                    val intent = Intent("com.test.activitytest.ACTION_START")
                    //TODO:  注意此处有个坑，在使用startActivity隐式跳转的，我们自定义的Activity的category中必须包含DEFUALT
                    //TODO： 因为startActivity会自动加上一个默认的DEFUALT
                    intent.addCategory("com.test.activitytest.TEST_2")
                    startActivity(intent)
                }
                //传递数据到2,并等待返回值
                R.id.btn_4 -> {
                    val intent = Intent(context, Test_Activity_2::class.java)
                    intent.putExtra("test_string__send_extra", "通过Intent传值")
                    startActivityForResult(intent, 101)
                }
                R.id.btn_5->{
                    Test_Activity_2.startActivity2(context,"静态启动","data2")
                }
            }
        }

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        //语法糖，java中使用get/set，在kotlin中都可以直接使用成员变量，会自动匹配get/set方法
        //例如java中这里应该使用item.getItem()
        when (item.itemId) {
            R.id.menu_add -> {
                Toast.makeText(this, "点击菜单Add", Toast.LENGTH_SHORT).show()
            }
            R.id.menu_remove -> {
                Toast.makeText(this, "点击菜单remove", Toast.LENGTH_SHORT).show()
            }
        }
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            101 -> {
                if (resultCode == Activity.RESULT_OK) {
                    Log.i("Test_Activity_1", "收到数据")
                    val stringExtra: String? = data?.getStringExtra("test_string__return_extra")
                    Log.i("Test_Activity_1", "收到数据${stringExtra?.toString()}")
                    tv_title.text = stringExtra?.toString() ?: "没有返回传值"

                }
            }
        }

    }


}
